<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/***** ST *****/
	const ERROR_STATE_INACTIVE=200;
	private $_uid;
	/***** TS *****/
	
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		$user = tSysUser::model()->find(
			'LOWER(su_name)=? AND su_password=?', 
			array($this->username, md5($this->password))
		);
		
		if($user === null){
			$this->errorCode=self::ERROR_UNKNOWN_IDENTITY;
		}
		else if((int)$user->su_activation != 1){
			$this->errorCode=self::ERROR_STATE_INACTIVE;
		}
		else{
			$this->_uid = $user->su_id; 
			$this->username = $user->su_name; 
			$this->errorCode = self::ERROR_NONE ;
		}
			
		return !$this->errorCode;
	}
	
	public function getId()
    {
        return $this->_uid;
    }

}